
package rss;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.MapperWrapper;

class XStreamFactory
{
	
	public static XStream createXStream() 
	{
		
		XStream xstream;
		
		xstream = new XStream(new PureJavaReflectionProvider(), new DomDriver())
		{
			protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {

                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        boolean b = definedIn != Object.class ? super.shouldSerializeMember(definedIn, fieldName) : false;
                        // System.out.println(fieldName + " : b = " + b);
                        return b;
                    }
                    
                };
            }		
		};
		
		xstream.addImplicitCollection(Channel.class, "items", Item.class);
		
		xstream.aliasField("item", Channel.class, "items");
		xstream.aliasField("itunes:duration", Item.class, "duration");
		
		xstream.alias("rss", RSS.class);
		xstream.alias("channel", Channel.class);
		xstream.alias("item", Item.class);
		xstream.alias("image", Image.class);
		xstream.alias("enclosure", Enclosure.class);
		
		xstream.aliasAttribute(Enclosure.class, "url", "url");
		xstream.aliasAttribute(Enclosure.class, "length", "length");
		xstream.aliasAttribute(Enclosure.class, "type", "type");
		
		return xstream;
		
	}

	
}
